import { createRouter ,createWebHashHistory,RouteRecordRaw} from 'vue-router'
import storage from "../utils/storage";


const routes:Array<RouteRecordRaw> = [
  {
    path:'/',
    component:()=>import('@/App.vue'),
    redirect:{
      path:'/login'
    },
    children:[
      {
        path:'login',
        component:()=>import('@/components/login.vue')
      },
      {
        path:'home',
        component:()=>import('@/components/layout/layout.vue'),
        children:[
          {
            path:'task',
            children:[
              {
                path:'patrolInspection',
                name:'巡查巡检任务',
                component:()=>import('@/views/task/patrolInspection/patrolInspection.vue')
              },
              {
                path:'patrolInspectionInfo',
                name:'巡查巡检任务详细',
                component:()=>import('@/views/task/patrolInspection/info/info.vue')
              },
              {
                path:'taskPlan',
                name:'巡查巡检编制方案',
                component:()=>import('@/views/task/plan/plan.vue')
              },
              {
                path:'dataAcquisition',
                name:'数据采集任务',
                component:()=>import('@/views/task/dataAcquisition/dataAcquisition.vue')
              },
              {
                path:'dataAcquisitionInfo',
                name:'数据采集任务详细',
                component:()=>import('@/views/task/dataAcquisition/info/info.vue')
              }
            ]
          }
        ]
      }
    ]
  },
 
]

const router = createRouter({
  history:createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') next()
  const tokenStr = storage.get('token',false)
  // 增加判断条件
  if (!tokenStr && to.path !== '/login'){
    console.log(tokenStr)
    return next('/login')
  }
  
  next()
})
export default router